package com.zmn.oms.dubbo.impl.zmn.bcustomer.order;

import org.apache.dubbo.config.annotation.Service;
import com.google.common.collect.Lists;
import com.zmn.common.constant.GlobalConsts;
import com.zmn.common.dto.ResponseDTO;
import com.zmn.common.utils.reflect.BeanMapper;
import com.zmn.common.utils.response.ResponseUtil;
import com.zmn.oms.business.interfaces.work.OrderWorkListBService;
import com.zmn.oms.common.constant.OrderConsts;
import com.zmn.oms.dubbo.dto.zmn.order.*;
import com.zmn.oms.dubbo.interfaces.zmn.bcustomer.order.ZsBCustomerOrderListRemoteService;
import com.zmn.oms.dubbo.utils.DubboConsts;
import com.zmn.oms.model.entity.serviceitem.OrderServiceItem;
import com.zmn.oms.model.entity.work.list.OrderWorkBCustomerQuery;
import com.zmn.base.oms.common.model.es.orderwork.EsOrderWork;
import com.zmn.oms.model.vo.work.detail.ZsOrderWorkDetailCommentVO;
import com.zmn.oms.model.vo.work.detail.ZsOrderWorkDetailContactVO;
import com.zmn.oms.model.vo.work.detail.ZsOrderWorkDetailVO;
import com.zmn.oms.services.interfaces.detail.OrderDetailService;
import com.zmn.oms.services.interfaces.serviceitem.OrderServiceItemService;
import com.zmn.oms.services.interfaces.work.OrderWorkService;
import com.zmn.oms.zmn.business.interfaces.work.ZsOrderWorkQueryBService;
import io.shardingsphere.api.HintManager;
import org.springframework.beans.factory.annotation.Autowired;

import java.util.List;

/**
 * 类描述：大客户工单查询
 *
 * @author lujia
 * @date 2019/01/05 11:04
 */
@Service(loadbalance = "roundrobin", timeout = 5000, retries = 2, version = DubboConsts.INTERFACE_VERSION, validation = "false")
public class ZsBCustomerOrderListRemoteServiceImpl implements ZsBCustomerOrderListRemoteService {

    @Autowired
    OrderWorkService orderWorkService;
    @Autowired
    OrderDetailService orderDetailService;

    @Autowired
    private OrderServiceItemService orderServiceItemService;
    @Autowired
    private ZsOrderWorkQueryBService orderWorkQueryBService;
    @Autowired
    private OrderWorkListBService orderWorkListBService;

    @Override
    public ResponseDTO<BCustomerOrderDetailDRO> findOrderDetail(Long orderId) {
        BCustomerOrderDetailDRO orderDetailDRO = new BCustomerOrderDetailDRO();
        try (HintManager hintManager = HintManager.getInstance()) {
            hintManager.setMasterRouteOnly();

            ZsOrderWorkDetailVO orderWorkDetail = orderWorkQueryBService.findOrderWorkDetail(orderId, orderId);
            BeanMapper.copy(orderWorkDetail, orderDetailDRO);
            orderDetailDRO.setWorkId(orderId);
            orderDetailDRO.setOrderId(orderId);

            // 评价
            ZsOrderWorkDetailCommentVO commentVO = orderWorkDetail.getUserCommentVO();
            if (commentVO != null) {
                orderDetailDRO.setComment(BeanMapper.map(commentVO, OrderCommentDRO.class));
            }

            // 联系信息
            ZsOrderWorkDetailContactVO contactVO = orderWorkDetail.getContactVO();
            if (contactVO != null) {
                orderDetailDRO.setContact(BeanMapper.map(contactVO, OrderContactDRO.class));
            }
        }

        // 服务项
        List<OrderServiceItem> orderServiceItemList = orderServiceItemService.listByOrderId(orderId);
        if (orderServiceItemList != null) {
            orderDetailDRO.setServiceItemList(BeanMapper.mapList(orderServiceItemList, OrderServItemDRO.class));
        }

        // 处理其它信息

        return ResponseUtil.responseSuccess(orderDetailDRO, GlobalConsts.SUCCESS_MSG);
    }

    @Override
    public ResponseDTO<BCustomerOrderPageDRO> listOrder(BCustomerOrderQueryDIO queryDIO) {

        OrderWorkBCustomerQuery query = BeanMapper.map(queryDIO, OrderWorkBCustomerQuery.class);
        query.setDefaultOrder(GlobalConsts.YES);
        query.setOrderField("receiveTime");

        // 查询大客户单
        query.setSourceType(OrderConsts.ORDER_SOURCE_TYPE_BCUSTOMER);

        // 查询
        List<EsOrderWork> esOrderWorks = orderWorkListBService.listPageOrderWorkByQuery(query);

        // 转 vo
        List<BCustomerOrderDRO> orderDROList = Lists.newArrayList();
        esOrderWorks.forEach((esOrderWork -> {
            BCustomerOrderDRO dro = BeanMapper.map(esOrderWork, BCustomerOrderDRO.class);
            dro.setUserName(esOrderWork.getContact().getUserName());
            dro.setContactName(esOrderWork.getContact().getContactName());
            orderDROList.add(dro);
        }));

        BCustomerOrderPageDRO pageDRO = new BCustomerOrderPageDRO();
        pageDRO.setTotalCount(query.getTotalCount());
        pageDRO.setList(orderDROList);
        return ResponseUtil.responseSuccess(pageDRO, GlobalConsts.SUCCESS_MSG);
    }
}
